package com.yumis.service.modules.upload.service.impl;

import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.exception.GlobalException;
import com.yumis.service.common.utils.oss.OSSBootUtil;
import com.yumis.service.common.vo.Response;
import com.yumis.service.config.OSSConfig;
import com.yumis.service.modules.upload.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Autowired
    private OSSConfig ossConfig;

    @Override
    public String uploadFile(String fileDir, MultipartFile file) {
        try {
            String path = OSSBootUtil.upload(ossConfig, file, fileDir);
            return path;
        } catch (IOException e) {
            throw new GlobalException(Response.buildResponse(HttpState.UPLOAD_IMAGE_EXCEPTION));
        }
    }

    @Override
    public void deleteImage(String url) {
        OSSBootUtil.delete(ossConfig,url);
    }
}
